package com.kj.xuyuan.util;

import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MD5Util {

    public static String md5(String txt) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 问题主要出在这里，Java的字符串是unicode编码，不受源码文件的编码影响；而PHP的编码是和源码文件的编码一致，受源码编码影响。
            md.update(txt.getBytes("utf-8"));
            StringBuffer buf = new StringBuffer();
            for (byte b : md.digest()) {
                buf.append(String.format("%02x", b & 0xff));
            }
            return buf.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String MapMd5(Map<String,String> map){
        Set set = map.keySet();
        Iterator iterator = set.iterator();
        StringBuffer sb = new StringBuffer();
        while (iterator.hasNext()){
            String key = (String) iterator.next();
            sb.append(key).append("=").append(map.get(key)).append("&");
        }
        sb.append("400d069a791d51ada8af3e6c2979bcd7");
        String str = sb.toString();
        LogUtils.e("TAG","str"+str);
        return md5(str);
    }
}
